<template>
  <el-dialog title="关联商户" :visible.sync="visible" :close-on-click-modal="false" append-to-body width="400px">
    <el-form ref="main-form" status-icon :model="mainQuery" :rules="rules" label-width="130px" label-position="left" :inline="false">
      <template>
        <el-form-item label="商户开户邮箱: " size="mini" prop="mchEmail">
          <el-input placeholder="" style="margin-bottom:10px;" v-model="mainQuery.mchEmail" clearable></el-input>
        </el-form-item>
      </template>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="close" size="mini">关 闭</el-button>
      <el-button @click="submit" type="primary" size="mini">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import ApiMain from "@/api/commission.salesTrans";

export default {
  mixins: [],
  components: {},

  data() {
    return {
      visible: false,
      loading: false,
      mainQueryRaw: null,
      mainQuery: {
        mchEmail: '',
      },
      rules: {
        mchEmail: [{ required: true, message: "请填写邮箱", trigger: "blur" }],
      },
      id: null,
      data: {},
    };
  },
  
  mounted() {
    this.mainQueryRaw = Object.copy(this.mainQuery);
  },

  methods: {
    show() {
      this.visible = true;
      this.fetchData();
    },

    close() {
      this.visible = false;
      this.$emit("dialog-close");
      this.reset()
    },

    verifyForm() {
      var bool = true;
      this.$refs['main-form'].validate((valid) => bool = valid);
      return bool;
    },

    async fetchData() {
      let data = await ApiMain.getBindedMch();
      this.mainQuery.mchEmail = data;
    },

    async submit() {
      if (!this.verifyForm()) return false;
      this.loading = true;
      let form = Object.copy(this.mainQuery)
      let data = await ApiMain.bindMch(form)
      this.$message({ message: '设置成功！', type: 'success' });
      this.loading = false;
      this.close()
    },

    reset() {
      this.$refs["main-form"] && this.$refs["main-form"].resetFields();
      this.id = null;
      this.data = null;
      this.mainQuery = {
        mchEmail: '',
      };
    },
  },
};
</script>

<style lang="scss" scoped>
.wire {
  height: 1px;
  width: 100%;
  background-color: black;
  margin: 20px 0px;
}
.tipsSuccess {
  color: #3ab54a;
}
.tipsCatch {
  color: red;
}
</style>
